package com.baizhi.action;

import com.baizhi.entity.Book;
import com.baizhi.service.BookService;
import com.baizhi.service.impl.BookServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class BookAction {
    private Integer pageNum;
    private Integer pageSize;

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void show() throws IOException {
        BookService bookService = new BookServiceImpl();
        List<Book> books = bookService.showBooks(pageNum, pageSize);//pageNum页要展示的数据
        PageInfo<Book> pageInfo = new PageInfo<>(books,10);
        int pages = pageInfo.getPages();

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setCharacterEncoding("utf-8");
        PrintWriter pw = resp.getWriter();
        //books集合 pages总页数
        //(属性1=值1&属性2=值2...),o2,o3,...:pages
        StringBuilder sb = new StringBuilder();
        for(Book b:books){
            //bookId=b.getBookId()&bookName=b.getBookName()&author=b.getAuthor()...
            sb.append("bookId=").append(b.getBookId())
                    .append("&bookName=").append(b.getBookName())
                    .append("&author=").append(b.getAuthor())
                    .append("&price=").append(b.getPrice())
                    .append("&stock=").append(b.getStock())
                    .append(",");
        }
        sb.deleteCharAt(sb.length()-1);
        sb.append(":").append(pages);

        pw.print(sb);

    }
}
